package logindata;

import helpdesk.CenteredFrame;

public class LoginFrame extends CenteredFrame {

    /**
     * Creates new form LoginFrame
     */
    public LoginFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        multipleAttempts = new javax.swing.JOptionPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        loginSubmitButton = new javax.swing.JButton();
        loginClearButton = new javax.swing.JButton();
        passwordBox = new javax.swing.JPasswordField();
        userNameBox = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Username:");

        jLabel2.setText("Password:");

        loginSubmitButton.setText("Submit");
        loginSubmitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginSubmitButtonActionPerformed(evt);
            }
        });

        loginClearButton.setText("Clear");
        loginClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginClearButtonActionPerformed(evt);
            }
        });

        passwordBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordBoxActionPerformed(evt);
            }
        });

        userNameBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userNameBoxActionPerformed(evt);
            }
        });
        userNameBox.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                userNameBoxKeyTyped(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel3.setText("Help Desk Login");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(93, 93, 93)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(passwordBox)
                            .addComponent(userNameBox, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(loginSubmitButton)
                        .addGap(18, 18, 18)
                        .addComponent(loginClearButton)))
                .addContainerGap(105, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addComponent(jLabel3)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(userNameBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(passwordBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginSubmitButton)
                    .addComponent(loginClearButton))
                .addContainerGap(60, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loginClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginClearButtonActionPerformed
        this.clearLoginFields();
    }//GEN-LAST:event_loginClearButtonActionPerformed

    private void loginSubmitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginSubmitButtonActionPerformed
        
        compareLoginData(userNameBox.getText(), passwordBox.getText());
    }//GEN-LAST:event_loginSubmitButtonActionPerformed

    private void passwordBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordBoxActionPerformed
        
        compareLoginData(userNameBox.getText(), passwordBox.getText());
    }//GEN-LAST:event_passwordBoxActionPerformed

    private void userNameBoxKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_userNameBoxKeyTyped
        
    }//GEN-LAST:event_userNameBoxKeyTyped

    private void userNameBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userNameBoxActionPerformed
        
        compareLoginData(userNameBox.getText(), passwordBox.getText());
    }//GEN-LAST:event_userNameBoxActionPerformed

    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoginFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoginFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoginFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoginFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                loginFrame = new LoginFrame();
                loginFrame.setVisible(true);
            }
        });
    }

    public String getUsername() {
        return userNameBox.getText();
    }

    public String getPassword() {
        return passwordBox.getText();
    }

    public void clearLoginFields() {
        userNameBox.setText("");
        passwordBox.setText("");
    }

    public void incorrectLogin() {
        numAttempts++;
        if (numAttempts > 2) {
            multipleAttempts.showMessageDialog(rootPane, "Too Many Authentication Attempts.");
            this.dispose();
            System.exit(0);
        } else {
            clearLoginFields();
        }
    }

    public logindata.User compareLoginData(String username, String password) {
        int permissions = Login.compare(username, password);
        if (permissions <= 0)
        {
            if (numAttempts < 2 && permissions > -5) {
                javax.swing.JOptionPane.showMessageDialog(null, "Invalid Login, Please try again", "Connection Error", javax.swing.JOptionPane.ERROR_MESSAGE, null);
            }
            incorrectLogin();
        } else {
            return new logindata.User(username, password, permissions);
        }
        return null;
    }

    public static void setLoginVisible(boolean visible) {
        loginFrame.setVisible(visible);
    }
    static LoginFrame loginFrame;
    private int numAttempts = 0;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton loginClearButton;
    private javax.swing.JButton loginSubmitButton;
    private javax.swing.JOptionPane multipleAttempts;
    private javax.swing.JPasswordField passwordBox;
    private javax.swing.JTextField userNameBox;
    // End of variables declaration//GEN-END:variables
}
